<?php

namespace App\Dao;

use App\Model\BusinessModel;

class BusinessesDao extends BaseDao
{
    public static function builder_query($params = []): \Illuminate\Database\Eloquent\Builder
    {
            $query = BusinessModel::query();

            $query->when(checkIsHasField($params,'businesses_id'),function ($query)use ($params){
                $query->where('businesses_id',$params['businesses_id']);
            });

            $query->when(checkIsHasField($params,'show'),function ($query)use ($params){
                $query->where('show',$params['show']);
            });

            return $query->orderByDesc('created_at');
    }

    public static function add($params = [])
    {
        if(self::builder_query()->create($params) === false){
            self::error('创建失败');
        }
    }



    public static function update($params = [])
    {
        if(self::builder_query(['businesses_id'=>self::deleteParamId($params,'businesses_id')])->update(self::filterFields($params,new BusinessModel())) === false){
            self::error('修改失败');
        }
    }

    public static function delete($params = [])
    {
        if(self::builder_query(['businesses_id'=>self::deleteParamId($params)])->delete() === false){
            self::error('删除失败');
        }
    }

    public static function businesses(array $params)
    {
        return self::builder_query($params)->paginate();
    }

    public static function info(array $all)
    {
        return self::builder_query($all)->first()->toArray();
    }
}
